package com.liu.nb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liu.nb.dto.PtUserDto;
import com.liu.nb.dto.TnspectUserDto;
import com.liu.nb.entity.Patroltask;
import com.liu.nb.entity.PatroltaskUser;
import com.liu.nb.entity.SysUser;
import com.liu.nb.service.IPatroltaskUserService;
import com.liu.nb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡检任务和巡检人的中间表 前端控制器
 * </p>
 *
 * @author yzl
 * @since 2021-08-31
 */
@RestController
@RequestMapping("/nb/patroltask-user")
public class PatroltaskUserController extends BaseController {

    /**
     * 加载巡检员列表
     * @return
     */
    @PostMapping("/getInspectUser")
    public Result getInspectUser(){
        List<SysUser> inspectUserList = userService.list(new QueryWrapper<SysUser>()
                .like("username", "xunjian"));
        TnspectUserDto[] userDtos=new TnspectUserDto[inspectUserList.size()];
        for (int i=0;i<userDtos.length;i++){
            userDtos[i]=new TnspectUserDto(inspectUserList.get(i).getId(),inspectUserList.get(i).getName());
        }
        return Result.success(userDtos);
    }

    /**
     * 加载巡检员列表
     * @return
     */
    @PostMapping("/getInspectUser2")
    public Result getInspectUser2(){
        List<SysUser> inspectUserList = userService.list(new QueryWrapper<SysUser>()
                .like("username", "xunjian"));

        return Result.success(inspectUserList);
    }

    /**
     * 线路管理员分配任务，根据分配的巡检员批量插入PatroltaskUser表
     *
     *
     * @return
     */
    @Transactional
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/savePatrolTaskUser/{ptid}")
    public Result savePatrolTaskUser(@RequestBody Integer []selectedUser,@PathVariable String ptid) {
        iPatroltaskUserService.remove(new QueryWrapper<PatroltaskUser>()
                .eq("partol_id",ptid));
        List<PatroltaskUser> patrolTaskUserList=new ArrayList<>();
        for (int i = 0; i < selectedUser.length; i++) {
            PatroltaskUser patroltaskUser = new PatroltaskUser();
            patroltaskUser.setUserId(selectedUser[i]);
            patroltaskUser.setPartolId(ptid);
            patrolTaskUserList.add(patroltaskUser);
        }
        boolean i = iPatroltaskUserService.saveBatch(patrolTaskUserList);
        return Result.success(i);
    }

    /**
     * 线路管理员取消任务
     *
     * @param patroltask
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/cancelTask")
    public Result cancelTask(@RequestBody Patroltask patroltask) {
        patroltask.setCancel(0);
        boolean b = iPatroltaskService.updateById(patroltask);
        return Result.success(b);
    }

    /**
     * 根据巡检任务编号查询执行任务巡检员
     * @param partOlId
     * @return
     */
    @PreAuthorize("hasAnyRole('xianluadmin','root')")
    @PostMapping("/selUserByTask/{partOlId}")
    public Result selUserByTask(@PathVariable String partOlId){
        List<SysUser> users = userService.list(new QueryWrapper<SysUser>().inSql("id", "SELECT user_id FROM patroltask_user WHERE partol_id=" + "'" + partOlId + "'"));
        String s = users.stream().map(i -> i.getName()).collect(Collectors.joining(","));
        TnspectUserDto[] userDtos=new TnspectUserDto[users.size()];
        for (int i=0;i<userDtos.length;i++){
            userDtos[i]=new TnspectUserDto(users.get(i).getId(),users.get(i).getName());
        }
        PtUserDto ptUserDto=new PtUserDto();
        ptUserDto.setTnspectUserDtos(userDtos);
        ptUserDto.setUsersName(s);
        return Result.success(ptUserDto);
    }


}
